#!/bin/bash

# 
# pxcropimageusingmask
#


functionname=`basename "$0"`

#####################################################################

function PrintHelp()
{
  echo "Crop the image using the bounding box of a mask."
  echo
  echo "Usage:"
	echo $functionname
	echo "  -in      input filename"
	echo "  [-out]   outputFilename, default in + CROPPED.mhd"
	echo "  -mask    mask filename"
	echo "  [-b?]    boundary around the mask for dimension ?, default 0"
	echo "  [-force] force to extract a region of size sz, pad if necessary"
}

#####################################################################

if [[ $# = 0 || $1 == "--help" ]]
then
  PrintHelp
  exit 64
fi

argv=($*)
for (( i = 0; i < $#; ++i ))
do
	arg=${argv[$i]}
	if [[ $arg == "-in" ]]
	then
		let i=$i+1
		in=${argv[$i]}
	fi
	if [[ $arg == "-out" ]]
	then
		let i=$i+1
		out=${argv[$i]}
	fi
	if [[ $arg == "-mask" ]]
	then
		let i=$i+1
		mask=${argv[$i]}
	fi
	if [[ $arg == "-b0" ]]; then let i=$i+1; b0=${argv[$i]}; fi
	if [[ $arg == "-b1" ]]; then let i=$i+1; b1=${argv[$i]}; fi
	if [[ $arg == "-b2" ]]; then let i=$i+1; b2=${argv[$i]}; fi
	if [[ $arg == "-force" ]]; then force="true"; fi
	if [[ $arg == "-z" ]]; then compress="true"; fi
done

#####################################################################

# Some sanity checks

if [[ ! -e $in ]]
then
  echo "ERROR: the input file "$in" does not exist"
  exit 1;
fi

if [[ ! -e $mask ]]
then
  echo "ERROR: the mask "$mask" does not exist"
  exit 1;
fi

#####################################################################

# Compute the bounding box
tmp="temp_1224978y9621.txt"
pxcomputeboundingbox -in $mask | dos2unix &> $tmp
if [[ $? != 0 ]]
then
	echo "ERROR: pxcomputeboundingbox failed:"
	echo `less $tmp`
	exit 1
fi

# Extract the minimum and maximum
pA=`less $tmp | grep "MinimumIndex"`
pA=${pA#MinimumIndex = [}
pA=${pA/]}; pA=${pA//,/}
pB=`less $tmp | grep "MaximumIndex"`
pB=${pB#MaximumIndex = [}
pB=${pB//]}; pB=${pB//,/}
rm $tmp

# Get the image size
sz=`pxgetimageinformation -in $in -sz`
sz=($sz)

# Convert to an array
pA=($pA); pB=($pB)

# Possibly add a boundary
if [[ $b0 != "" ]]
then
	let "pA[0] -= $b0"
	let "pB[0] += $b0"
	if [[ ${pA[0]} -lt 0 ]]; then pA[0]=0; fi
	if [[ ${pB[0]} -gt ${sz[0]} ]]; then pB[0]=${sz[0]}; fi
fi

if [[ $b1 != "" ]]
then
	let "pA[1] -= $b1"
	let "pB[1] += $b1"
	if [[ ${pA[1]} -lt 0 ]]; then pA[1]=0; fi
	if [[ ${pB[1]} -gt ${sz[1]} ]]; then pB[1]=${sz[1]}; fi
fi

if [[ $b2 != "" ]]
then
	let "pa = ${pA[2]} - $b2"; pA[2]=$pa
	let "pb = ${pB[2]} + $b2"; pB[2]=$pb
	#let "pA[2] -= $b2"
	#let "pA[2] += $b2"
	if [[ ${pA[2]} -lt 0 ]]; then pA[2]=0; fi
	if [[ ${pB[2]} -gt ${sz[2]} ]]; then pB[2]=${sz[2]}; fi
fi

# Convert back to a string
pA="${pA[@]}"; pB="${pB[@]}"

# Construct the arguments for pxcropimage
args="-in "$in" -pA "$pA" -pB "$pB
if [[ $out != "" ]]
then
	args=$args" -out "$out
fi
if [[ $force == "true" ]]
then
	args=$args " -force"
fi

# Crop the image
pxcropimage $args

# Compress image
if [[ $compress == "true" ]]
then
	pxcompressimage $out >> /dev/null
fi

# return a value
exit 0

